import Vue from "vue"
import Vuex from 'vuex'

Vue.use(Vuex)

// 导航栏Aside收缩模块
const AsideCollapse = {
    namespaced: true,	// 开启命名空间
    state: { 
        isCollapse: false
    },
    mutations: { 
       CollapseMenu(state){
        // 改变收缩栏的方法
        state.isCollapse= !state.isCollapse;
       }
    },
    actions: { }
  }

// 面包屑模块
const BreadCrumbs = {
    namespaced: true,	// 开启命名空间
    state: { 
        menuList: [
            {
                id: '1',
                path: "/backHome/",
                name: "Index",
                label: "首页",
            }
        ],
        // 当前menu高亮显示  主页高亮代表null
        crrentMenu: null
    },
    mutations: { 
       SelectMenu(state, menu){
        // console.log(menu)
            // 不是主页
            if(menu.name != 'Index'){
                state.crrentMenu=menu;
                // 查看数组中是否有此menu路径
                const result = state.menuList.findIndex(item => item.name === menu.name);
                if(result == -1){
                    // 没有将此menu加入数组
                    state.menuList.push(menu);
                }
            }else{
                //是主页
                state.crrentMenu=null;
            }
            // console.log(state.menuList)
       },
       CloseTag(state, val){
            // console.log(111)
            const result=state.menuList.findIndex(item => item.name === val.name);
            state.menuList.splice(result, 1);
       },
       ClearMenu(state){
        // 退出登录时重置面包屑
        state.menuList=[
            {
                id: '1',
                path: "/backHome/",
                name: "Index",
                label: "首页",
            }
        ]
       }
    },
    actions: { }
  }
// 用户信息
const UserInfo ={
    namespaced: true,	// 开启命名空间
    state: { 
        userInfo: '',
        userImg: '',
        defaultUserImg: 'https://img0.baidu.com/it/u=2933144277,475331862&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500'
    },
    mutations: { 
        // 更新用户信息
        updataUserInfo(state, user){
         state.userInfo=user
        },
        // 更新用户头像
       updataUserImg(state, userimg){
        state.userImg=userimg;
       },
    },
}

const store = new Vuex.Store({
    modules: {
        AsideCollapse,
        BreadCrumbs,
        UserInfo
    }
})

export default store